﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbAddressBookDal
	{
		public DbAddressBookDal()
		{
		}
 		#region Insert
 		public static int Insert(DbAddressBook dbAddressBook)
		{	
			string sql = @"Insert Into DbAddressBook (
					Address,
					Address2,
					ZipCode,
					City,
					State,
					Country,
					Tel,
					AddressType,
					UserId,
					Sex,
					FirstName,
					LastName
				) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}',{7},{8},{9},'{10}','{11}')";
			sql = string.Format(sql,
					SqlHelper.String(dbAddressBook.Address),
					SqlHelper.String(dbAddressBook.Address2),
					SqlHelper.String(dbAddressBook.ZipCode),
					SqlHelper.String(dbAddressBook.City),
					SqlHelper.String(dbAddressBook.State),
					SqlHelper.String(dbAddressBook.Country),
					SqlHelper.String(dbAddressBook.Tel),
					dbAddressBook.AddressType,
					dbAddressBook.UserId,
					dbAddressBook.Sex,
					SqlHelper.String(dbAddressBook.FirstName),
					SqlHelper.String(dbAddressBook.LastName)
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbAddressBook dbAddressBook)
		{	
			string sql = @"Update DbAddressBook set 

					Address= '{0}',
					Address2= '{1}',
					ZipCode= '{2}',
					City= '{3}',
					State= '{4}',
					Country= '{5}',
					Tel= '{6}',
					AddressType= {7},
					UserId= {8},
					Sex= {9},
					FirstName= '{10}',
					LastName= '{11}'
				where

					AddressId= {12}";
			sql = string.Format(sql,
					SqlHelper.String(dbAddressBook.Address),
					SqlHelper.String(dbAddressBook.Address2),
					SqlHelper.String(dbAddressBook.ZipCode),
					SqlHelper.String(dbAddressBook.City),
					SqlHelper.String(dbAddressBook.State),
					SqlHelper.String(dbAddressBook.Country),
					SqlHelper.String(dbAddressBook.Tel),
					dbAddressBook.AddressType,
					dbAddressBook.UserId,
					dbAddressBook.Sex,
					SqlHelper.String(dbAddressBook.FirstName),
					SqlHelper.String(dbAddressBook.LastName),
					dbAddressBook.AddressId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 addressId)
		{
		
			string sql = @"Delete from DbAddressBook where 
					AddressId= {0}";
			sql = string.Format(sql,
					addressId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbAddressBook a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbAddressBook Select(Int32 addressId)
		{
			DbAddressBook dbAddressBook = null;
			string sql = @"select a.* from DbAddressBook a  where
					a.AddressId= {0}
";
			sql = string.Format(sql,
					addressId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbAddressBook = new DbAddressBook(ds.Tables[0].Rows[0]);					
			return dbAddressBook;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbAddressBook a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbAddressBook a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbAddressBook a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
